package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.system.domain.IotInterfaceDeviceBo;
import com.lechi.web.system.mapper.iot.IotInterfaceDeviceMapper;
import com.lechi.web.system.service.IIotInterfaceDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 通用接口和设备关系Service业务层处理
 *
 * @author yangguang
 * @date 2025-03-31
 */
@Service
public class IotInterfaceDeviceServiceImpl implements IIotInterfaceDeviceService {
    @Autowired
    private IotInterfaceDeviceMapper iotInterfaceDeviceMapper;

    /**
     * 查询通用接口和设备关系列表
     *
     * @param iotInterfaceDevice 通用接口和设备关系
     * @return 通用接口和设备关系
     */
    @Override
    public List<Long> selectIotInterfaceDeviceList(IotInterfaceDeviceBo iotInterfaceDevice) {
        try {
            return iotInterfaceDeviceMapper.selectIotInterfaceDeviceList(iotInterfaceDevice);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询通用接口和设备关系列表", method, iotInterfaceDevice));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }


    /**
     * 新增通用接口和设备关系
     *
     * @param iotInterfaceDevice 通用接口和设备关系
     * @return 结果
     */
    @Override
    public int insertIotInterfaceDevice(List<IotInterfaceDeviceBo> iotInterfaceDevice) {
        try {
            return iotInterfaceDeviceMapper.insertIotInterfaceDevice(iotInterfaceDevice);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增通用接口和设备关系", method, iotInterfaceDevice));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 批量删除通用接口和设备关系
     *
     * @param interfaceId 需要删除的通用接口和设备关系主键
     * @return 结果
     */
    @Override
    public int deleteIotInterfaceDeviceByInterfaceId(Long interfaceId) {
        try {
            return iotInterfaceDeviceMapper.deleteIotInterfaceDeviceByInterfaceId(interfaceId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "删除通用接口和设备关系", method, interfaceId));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }
}
